package com.wenlin.myproduction.ui;

import android.content.Context;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hjq.shape.view.ShapeTextView;
import com.lxj.xpopup.core.CenterPopupView;
import com.wenlin.myproduction.R;
import com.wenlin.myproduction.util.Checker;
import com.wenlin.myproduction.util.UI;

/**
 * @desc: 通用对话框
 * @author: 文琳
 * @time: 2025/11/13 周四 15:38
 */
public class CommonDialog extends CenterPopupView {
    private String title, content, leftText, rightText;
    private OnClickListener leftClickListener, rightClickListener;

    public CommonDialog(@NonNull Context context) {
        super(context);
    }

    public CommonDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public CommonDialog setContent(String content) {
        this.content = content;
        return this;
    }

    public CommonDialog setLeftClickListener(OnClickListener leftClickListener) {
        this.leftClickListener = leftClickListener;
        return this;
    }

    public CommonDialog setLeftText(String leftText) {
        this.leftText = leftText;
        return this;
    }

    public CommonDialog setRightClickListener(OnClickListener rightClickListener) {
        this.rightClickListener = rightClickListener;
        return this;
    }

    public CommonDialog setRightText(String rightText) {
        this.rightText = rightText;
        return this;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_common;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        TextView tvTitle = findViewById(R.id.tv_title);
        TextView tvContent = findViewById(R.id.tv_content);
        if (Checker.hasWord(title)) UI.setText(tvTitle, title);
        if (Checker.hasWord(content)) UI.setText(tvContent, content);
        ShapeTextView tvLeft = findViewById(R.id.tv_left);
        ShapeTextView tvRight = findViewById(R.id.tv_right);
        if (Checker.hasWord(leftText)) UI.setText(tvLeft, leftText);
        if (Checker.hasWord(rightText)) UI.setText(tvRight, rightText);
        UI.setOnClickListener(tvLeft, v -> {
            if (leftClickListener != null) leftClickListener.onClick(v);
            dismiss();
        });
        UI.setVisibleGone(Checker.hasWord(rightText), findViewById(R.id.v_vertical), tvRight);
        UI.setOnClickListener(tvRight, v -> {
            if (rightClickListener != null) rightClickListener.onClick(v);
            dismiss();
        });
    }
}
